home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Mac / scripts / mkestrres.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  3.4 KB  |  151 lines

  1. """Parse sys/errno.h and Errors.h and create Estr resource"""
  2.  
  3. import regex
  4. import macfs
  5. import string
  6. import Res
  7. import os
  8.  
  9. READ = 1
  10. WRITE = 2
  11. smAllScripts = -3
  12.  
  13. ERRNO_PROG="#define[ \t]+" \
  14.            "\([A-Z0-9a-z_]+\)" \
  15.            "[ \t]+" \
  16.            "\([0-9]+\)" \
  17.            "[ \t]*/\*[ \t]*" \
  18.            "\(.*\)" \
  19.            "[ \t]*\*/"
  20.            
  21. ERRORS_PROG="[ \t]*" \
  22.             "\([A-Z0-9a-z_]+\)" \
  23.             "[ \t]*=[ \t]*" \
  24.             "\([-0-9]+\)" \
  25.             "[, \t]*/\*[ \t]*" \
  26.             "\(.*\)" \
  27.             "[ \t]*\*/"
  28.            
  29. ERRORS_PROG_2="[ \t]*" \
  30.             "\([A-Z0-9a-z_]+\)" \
  31.             "[ \t]*=[ \t]*" \
  32.             "\([-0-9]+\)" \
  33.             "[, \t]*"
  34.  
  35. def Pstring(str):
  36.     if len(str) > 255:
  37.         raise ValueError, 'String too large'
  38.     return chr(len(str))+str
  39.     
  40. def writeestr(dst, edict):
  41.     """Create Estr resource file given a dictionary of errors."""
  42.     
  43.     os.unlink(dst.as_pathname())
  44.     Res.FSpCreateResFile(dst, 'RSED', 'rsrc', smAllScripts)
  45.     output = Res.FSpOpenResFile(dst, WRITE)
  46.     Res.UseResFile(output)
  47.     for num in edict.keys():
  48.         res = Res.Resource(Pstring(edict[num][0]))
  49.         res.AddResource('Estr', num, '')
  50.         res.WriteResource()
  51.     Res.CloseResFile(output)
  52.     
  53. def writepython(fp, dict):
  54.     k = dict.keys()
  55.     k.sort()
  56.     for i in k:
  57.         fp.write("%s\t=\t%d\t#%s\n"%(dict[i][1], i, dict[i][0]))
  58.     
  59.  
  60. def parse_errno_h(fp, dict):
  61.     errno_prog = regex.compile(ERRNO_PROG)
  62.     for line in fp.readlines():
  63.         if errno_prog.match(line) > 0:
  64.             number = string.atoi(errno_prog.group(2))
  65.             name = errno_prog.group(1)
  66.             desc = string.strip(errno_prog.group(3))
  67.             
  68.             if not dict.has_key(number):
  69.                 dict[number] = desc, name
  70.             else:
  71.                 print 'DUPLICATE', number
  72.                 print '\t', dict[number]
  73.                 print '\t', (desc, name)
  74.                                 
  75. def parse_errors_h(fp, dict):
  76.     errno_prog = regex.compile(ERRORS_PROG)
  77.     errno_prog_2 = regex.compile(ERRORS_PROG_2)
  78.     for line in fp.readlines():
  79.         match = 0
  80.         if errno_prog.match(line) > 0:
  81.             number = string.atoi(errno_prog.group(2))
  82.             name = errno_prog.group(1)
  83.             desc = string.strip(errno_prog.group(3))
  84.             match=1
  85.         elif errno_prog_2.match(line) > 0:
  86.             number = string.atoi(errno_prog_2.group(2))
  87.             name = errno_prog_2.group(1)
  88.             desc = name
  89.             match=1
  90.         if match:
  91.             if number > 0: continue
  92.             
  93.             if not dict.has_key(number):
  94.                 dict[number] = desc, name
  95.             else:
  96.                 print 'DUPLICATE', number
  97.                 print '\t', dict[number]
  98.                 print '\t', (desc, name)
  99.                 if len(desc) > len(dict[number][0]):
  100.                     print 'Pick second one'
  101.                     dict[number] = desc, name
  102.             
  103. def main():
  104.     dict = {}
  105.     fss, ok = macfs.PromptGetFile("Where is errno.h?")
  106.     if not ok: return
  107.     fp = open(fss.as_pathname())
  108.     parse_errno_h(fp, dict)
  109.     fp.close()
  110.     
  111.     fss, ok = macfs.PromptGetFile("Select 2nd errno.h or cancel")
  112.     if not ok: return
  113.     fp = open(fss.as_pathname())
  114.     parse_errno_h(fp, dict)
  115.     fp.close()
  116.     
  117.     fss, ok = macfs.PromptGetFile("Where is Errors.h?")
  118.     if not ok: return
  119.     fp = open(fss.as_pathname())
  120.     parse_errors_h(fp, dict)
  121.     fp.close()
  122.     
  123.     if not dict:
  124.         return
  125.         
  126.     fss, ok = macfs.StandardPutFile("Resource output file?", "errors.rsrc")
  127.     if ok:
  128.         writeestr(fss, dict)
  129.     
  130.     fss, ok = macfs.StandardPutFile("Python output file?", "macerrors.py")
  131.     if ok:
  132.         fp = open(fss.as_pathname(), "w")
  133.         writepython(fp, dict)
  134.         fp.close()
  135.         fss.SetCreatorType('Pyth', 'TEXT')
  136.  
  137.     fss, ok = macfs.StandardPutFile("Text output file?", "errors.txt")
  138.     if ok:
  139.         fp = open(fss.as_pathname(), "w")
  140.         
  141.         k = dict.keys()
  142.         k.sort()
  143.         for i in k:
  144.             fp.write("%d\t%s\t%s\n"%(i, dict[i][1], dict[i][0]))
  145.         fp.close()
  146.  
  147.     
  148. if __name__ == '__main__':
  149.     main()
  150.     
  151.